package edu.ynu.se.xiecheng.achitectureclass.camera.service;

import edu.ynu.se.xiecheng.achitectureclass.device.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.device.service.DeviceService;
import edu.ynu.se.xiecheng.achitectureclass.camera.dao.CameraImageDAO;
import edu.ynu.se.xiecheng.achitectureclass.camera.entity.CameraImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class CameraService {
    
    private final String TOKEN_URL = "http://42.193.14.241:7000/ysapi/subAccount/getToken";
    private final String CAPTURE_URL = "https://open.ys7.com/api/lapp/device/capture";
    private final String ACCOUNT_ID = "1ca7f0ab24ba41b291346347ec30140e";  // 使用文档提供的accountId
    
    @Autowired
    private DeviceService deviceService;
    
    @Autowired
    private RestTemplate restTemplate;
    
    @Autowired
    private CameraImageDAO cameraImageDAO;

    public Map<String, Object> getYsToken() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        String requestBody = String.format("{\"accountId\":\"%s\"}", ACCOUNT_ID);
        
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        
        ResponseEntity<Map> response = restTemplate.postForEntity(
            TOKEN_URL,
            request,
            Map.class
        );
        
        return response.getBody();
    }

    public Map<String, Object> captureImage(String deviceNumber, String accessToken) {
        // 先获取设备信息
        Device device = deviceService.getDeviceByNumber(deviceNumber);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
        
        // 调用萤石云API
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("accessToken", accessToken);
        map.add("deviceSerial", deviceNumber);
        map.add("channelNo", "1");
        map.add("quality", "1");
        
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        
        ResponseEntity<Map> response = restTemplate.postForEntity(
            CAPTURE_URL,
            request,
            Map.class
        );
        
        Map<String, Object> responseBody = response.getBody();
        
        // 保存图片记录到数据库
        if (responseBody != null && responseBody.containsKey("data")) {
            Map<String, Object> data = (Map<String, Object>) responseBody.get("data");
            if (data.containsKey("picUrl")) {
                String imageUrl = (String) data.get("picUrl");
                
                CameraImage cameraImage = new CameraImage();
                cameraImage.setDevice(device);
                cameraImage.setImageUrl(imageUrl);
                cameraImage.setCaptureTime(LocalDateTime.now());
                
                cameraImageDAO.save(cameraImage);
            }
        }
        
        return responseBody;
    }
} 